package com.huopaolan.yohuoshop.Mvp.View.Fragment;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.OnScrollListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.core.ImagePipeline;
import com.huopaolan.lib_core.Base.BaseFragment;
import com.huopaolan.yohuoshop.Adapter.MyHeadAdapter;
import com.huopaolan.yohuoshop.Bean.ShowEntity;
import com.huopaolan.yohuoshop.Dao.Service.DBService;
import com.huopaolan.yohuoshop.Di.Compont.DaggerMyFragmentCompont;
import com.huopaolan.yohuoshop.Di.Modules.MyFragmentModules;
import com.huopaolan.yohuoshop.Mvp.Contract.MyFragmentContract;
import com.huopaolan.yohuoshop.Mvp.Presenter.MyFragmentPresenter;
import com.huopaolan.yohuoshop.Mvp.View.Activity.CollectionActivity;
import com.huopaolan.yohuoshop.Mvp.View.Activity.LoginActivity;
import com.huopaolan.yohuoshop.Mvp.View.Activity.OutActivity;
import com.huopaolan.yohuoshop.Mvp.View.Activity.PersonalDetailsActivity;
import com.huopaolan.yohuoshop.NetWork.API;
import com.huopaolan.yohuoshop.R;
import com.huopaolan.yohuoshop.Sp.SpUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MyFragment extends BaseFragment<MyFragmentPresenter> implements MyFragmentContract.MyIView, OnClickListener {
    private MyHeadAdapter adapter;
    private View headView;
    private RecyclerView my_recycler;
    private ImageView image_setting;
    private Button login_button;
    private RelativeLayout relativeLayout, relative_view, relative_login;
    private SimpleDraweeView user_head;
    private TextView user_names, user_num, collection_count;
    private LinearLayout shop_collection_button;
    private ImageView qr_code, my_message;
    private int totalY = 0;
    private boolean spRead;
    private int bannerHeight = 0;
    //values集合
    List<ShowEntity.Values> valuesList = new ArrayList<>();

    @Override
    public View createView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_my, null);
    }

    @Override
    public void initView(View view) {
        EventBus.getDefault().register(this);
        //初始化控件
        image_setting = view.findViewById(R.id.my_setting);
        my_recycler = view.findViewById(R.id.my_recycler);
        headView = View.inflate(getActivity(), R.layout.head_self_layout, null);
        shop_collection_button = headView.findViewById(R.id.shop_collection_button);
        collection_count = headView.findViewById(R.id.spsc);
        login_button = headView.findViewById(R.id.login);
        relative_login = headView.findViewById(R.id.user_login_message);
        relative_view = headView.findViewById(R.id.rela_change_bg);
        user_names = headView.findViewById(R.id.user_name);
        user_num = headView.findViewById(R.id.user_num);
        user_head = headView.findViewById(R.id.user_head);
        qr_code = view.findViewById(R.id.my_qr_code);
        my_message = view.findViewById(R.id.my_message);
        shop_collection_button.setOnClickListener(this);
        my_message.setOnClickListener(this);
        qr_code.setOnClickListener(this);
        user_head.setOnClickListener(this);
        login_button.setOnClickListener(this);
        image_setting.setOnClickListener(this);
        relativeLayout = view.findViewById(R.id.head_alpha);
        headView.measure(0, 0);
        bannerHeight = headView.getMeasuredHeight();
        //添加适配器
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2, GridLayoutManager.VERTICAL, false);
        my_recycler.setLayoutManager(gridLayoutManager);
        my_recycler.addOnScrollListener(new MyListener());
        adapter = new MyHeadAdapter(R.layout.item_my_shop);
        adapter.setHeaderView(headView, 0);
        my_recycler.setAdapter(adapter);
    }

    @Override
    public void onStart() {
        Window window = getActivity().getWindow();
        window.setStatusBarColor(Color.BLACK);
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        //刷新ui
        spRead = SpUtils.getInstance().SpRead();
        if (spRead == true) {
            relative_view.setVisibility(View.GONE);
            relative_login.setVisibility(View.VISIBLE);
            String spReadUser = SpUtils.getInstance().SpReadUser();
            Cursor user = DBService.getDbService().query("User", "user_id=?", new String[]{spReadUser});
            while (user.moveToNext()) {
                String name = user.getString(user.getColumnIndex("nick_name"));
                String users = user.getString(user.getColumnIndex("user_name"));
                String user_heads = user.getString(user.getColumnIndex("user_head"));
                Log.d("Query", "----->" + name);
                Log.d("QueryImage", "----->" + user_heads);
                user_names.setText("" + name);
                user_num.setText("" + users);
                Uri uri = Uri.parse(API.BaseUrl + user_heads);
                ImagePipeline imagePipeline = Fresco.getImagePipeline();
                imagePipeline.evictFromMemoryCache(uri);
                imagePipeline.evictFromDiskCache(uri);
                imagePipeline.evictFromDiskCache(uri);
                user_head.setImageURI(uri);
            }
        } else {
            relative_login.setVisibility(View.GONE);
            relative_view.setVisibility(View.VISIBLE);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(String name) {
        SpUtils.getInstance().SpWriteUser(name);
    }

    @Override
    public void initData() {
        p.MyRequest();
    }

    @Override
    public void setUpDagger() {
        DaggerMyFragmentCompont.builder()
                .myFragmentModules(new MyFragmentModules(this))
                .build().inject(this);
    }

    @Override
    public void myShopSuccess(Map<String, List> map) {
        valuesList.addAll(map.get("valuesList"));
        Toast.makeText(getActivity(), "" + valuesList.get(0).goods_img_path, Toast.LENGTH_SHORT).show();
        adapter.addData(valuesList);
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login:
                startActivity(new Intent(getActivity(), LoginActivity.class));
                break;
            case R.id.my_setting:
                //更改设置
                if (spRead == true) {
                    startActivity(new Intent(getActivity(), OutActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
                break;
            case R.id.user_head:
                //更改用户个人信息
                if (spRead == true) {
                    startActivity(new Intent(getActivity(), PersonalDetailsActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
                break;
            case R.id.shop_collection_button:
                startActivity(new Intent(getActivity(), CollectionActivity.class));
                break;
        }
    }

    //滑动监听
    private class MyListener extends OnScrollListener {
        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            totalY = dy;
            float alpha = (float) totalY / (float) bannerHeight;
            //alpha 0-1;
            if (alpha <= 1)
                alpha = 0.9f;
            if (alpha >= 0)
                alpha = 0.9f;
            relativeLayout.setAlpha(alpha);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
